<template>
  <div id="app">
    <div class="container">
      <header>
        <div class="top-background">
          <h2>第{{ ruaNumber }}期</h2>
          <h1>我与软件有个约会</h1>
        </div>
      </header>

      <div class="slider">
        <div v-for="(slide, index) in slides" :key="index" :class="['slide', { active: index === currentSlide }]">
          <img :src="slide.src" class="img">
        </div>
        <div class="indicator">
          <span v-for="(slide, index) in slides" :key="index"
            :class="['indicator-dot', { active: index === currentSlide }]" @click="changeSlide(index)"></span>
        </div>
      </div>

      <div class="contestants">
        <h2 class="contestants-title">选手风采</h2>
      </div>
      <div class="row" v-for="(row, rowIndex) in rows" :key="rowIndex">
        <div class="contestant" v-for="(contestant, index) in row" :key="index" :data-index="rowIndex * 2 + index">
          <p>{{ (rowIndex * 2 + index + 1) }}号选手</p>
          <p>{{ contestant.matName }}</p>
          <img :src="contestant.matAvatar" alt="选手头像">
          <button @click="openScoreDialog(rowIndex * 2 + index)" class="pill-button"
            :disabled="!canScore || contestant.hasBeenScored">评分</button>
        </div>
      </div>

      <el-dialog :title="`给 ${currentContestant.matName || '未知选手'} 打分`" :visible.sync="dialogVisible"
        :style="{ width: '300px', marginLeft: 'auto', marginRight: 'auto' }" :modal="false">
        <el-input v-model="score" type="text" min="1" max="100" placeholder="请输入分数"></el-input>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitScore" :disabled="!canScore">确 定</el-button>
        </span>
      </el-dialog>

      <footer>
        <div class="home-button1">
          <button class="home-button" @click="navigateTo('首页.html')"></button>
          <p class="home-button-text">首页</p>
        </div>
        <div class="score-button1">
          <button class="score-button" @click="navigateTo('查看评分.html')"></button>
          <p class="score-button-text">查看评分</p>
        </div>
        <div class="exit-button1">
          <button class="exit-button" @click="navigateTo('#')"></button>
          <p class="exit-button-text">退出</p>
        </div>
      </footer>
      <div class="bottom-message">已经划到最底部啦~</div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

export default {
  data() {
    return {
      contestants: [],
      ruanyueList: [],
      dialogVisible: false,
      currentContestantIndex: null,
      score: '',
      currentContestant: {},
      ruaNumber: '',
      canScore: true,
      slides: [
        { src: '../assets/图片1.png' },
        { src: '../assets/图片2.png' },
        { src: '../assets/图片4.png' },
        { src: '../assets/图片5.png' }
      ],
      currentSlide: 0
    };
  },
  computed: {
    rows() {
      const rows = [];
      for (let i = 0; i < this.contestants.length; i += 2) {
        rows.push(this.contestants.slice(i, i + 2));
      }
      return rows;
    }
    , created() {
      this.fetchRuanyueData();
    }
  },
  methods: {
    openScoreDialog(index) {
      if (this.contestants[index].hasBeenScored) {
        alert('该选手已被评分');
        return;
      }
      this.currentContestantIndex = index;
      this.currentContestant = this.contestants[index];
      this.dialogVisible = true;
      this.score = '';
    },
    submitScore() {
      const finalScore = parseFloat(this.score);

      if (isNaN(finalScore) || finalScore < 1 || finalScore > 100) {
        alert('请输入1到100之间的分数');
        return;
      }

      const userData = localStorage.getItem('user');
      const judId = userData ? JSON.parse(userData).judId : '';
      console.log(judId)

      const scoreData = {
        judId: 1,
        plaId: this.currentContestant.id,
        recScore: finalScore
      };
      request.post("/record/score", scoreData).then(res => {
        console.log('评分成功:', res.data);
        this.dialogVisible = false;
        this.contestants[this.currentContestantIndex].hasBeenScored = true;

        this.$nextTick(() => {
          const buttonElement = document.querySelector(`.contestant[data-index="${this.currentContestantIndex}"] .pill-button`);
          if (buttonElement) {
            buttonElement.classList.add('scored');
          }
        });
      })
        /* axios.post('http://192.168.100.198:8083/api/record/save', scoreData)
          .then(response => {
            console.log('评分成功:', response.data);
            alert('评分成功');
            this.dialogVisible = false;
           
          }) */
        .catch(error => {
          console.error('评分失败', error);
          alert('评分失败，请重试');
        });
    },
    fetchRuanyueData() {
      request.get("/judge/list").then(res => {
        console.log('Ruanyue data:', res.data);
        console.log('Response data:', response.data);
        this.ruanyueList = response.data.data;
        if (response.data.data && response.data.data[0] && response.data.data[0].ruaNumber) {
          this.ruaNumber = response.data.data[0].ruaNumber;
          console.log('ruaNumber:', this.ruaNumber);
        } else {
          console.error('Response data does not contain ruaNumber');
        }
      })
        /* axios.get('http://192.168.100.198:8083/api/ruanyue/list')
          .then(response => {
            console.log('Response data:', response.data);
            this.ruanyueList = response.data.data;
            if (response.data.data && response.data.data[0] && response.data.data[0].ruaNumber) {
              this.ruaNumber = response.data.data[0].ruaNumber;
              console.log('ruaNumber:', this.ruaNumber);
            } else {
              console.error('Response data does not contain ruaNumber');
            }
          }) */
        .catch(error => {
          console.error('获取 ruanyue 数据失败', error);
        });
    },
    fetchContestantsData() {
      request.get("/player/list").then(res => {
        console.log('Contestants data:', res.data);
        if (response.data && response.data.data) {
          this.contestants = response.data.data.map((contestant, index) => ({
            ...contestant,
            hasBeenScored: false,
            index: index
          }));
          console.log('Contestants data assigned:', this.contestants);
        } else {
          console.error('Response data does not contain expected structure');
        }
      })
        /*  axios.get('http://192.168.100.198:8083/api/match/list')
           .then(response => {
             console.log('Match data:', response.data);
             if (response.data && response.data.data) {
               this.contestants = response.data.data.map((contestant, index) => ({
                 ...contestant,
                 hasBeenScored: false,
                 index: index
               }));
               console.log('Contestants data assigned:', this.contestants);
             } else {
               console.error('Response data does not contain expected structure');
             }
           }) */
        .catch(error => {
          console.error('获取选手数据失败', error);
          if (error.response) {
            console.error('Server responded with status:', error.response.status);
            console.error('Server response data:', error.response.data);
          } else if (error.request) {
            console.error('No response received from server:', error.request);
          } else {
            console.error('Error setting up request:', error.message);
          }
        });
    },
    openAllScoreChannels() {
      this.contestants.forEach(contestant => {
        contestant.hasBeenScored = false;
      });
      this.canScore = true;
      alert('所有选手的评分通道已打开');
    },
    navigateTo(url) {
      window.location.href = url;
    },
    changeSlide(index) {
      this.currentSlide = index;
    }
  },
  mounted() {
    this.fetchRuanyueData();
    this.fetchContestantsData();

    this.socket = new WebSocket('ws://your-websocket-url');

    this.socket.onopen = () => {
      console.log('WebSocket connection established');
    };

    this.socket.onmessage = (event) => {
      const message = JSON.parse(event.data);
      if (message.type === 'score_channel') {
        if (message.action === 'open') {
          this.canScore = true;
        } else if (message.action === 'close') {
          this.canScore = false;
        }
      }
    };

    this.socket.onclose = () => {
      console.log('WebSocket connection closed');
    };

    this.socket.onerror = (error) => {
      console.error('WebSocket error:', error);
    };

    // 初始化时立即显示第一张图片
    this.changeSlide(0);
    // 使用 setInterval 启动轮播
    setInterval(() => {
      this.currentSlide = (this.currentSlide + 1) % this.slides.length;
    }, 3000);
  }
};
</script>

<style scoped>
body,
html {
  margin: 0;
  padding: 0;
  overflow-x: hidden;
}

/* 其他元素的背景色设置为透明 */
header {
  text-align: center;
  padding: 5px 0;
  background-color: transparent;
  /* 设置背景色为透明 */
}

.logo {
  width: 400px;
  max-width: 100%;
  margin: 10px auto;
}

.carousel {
  position: relative;
  overflow: hidden;
  width: 100%;
  max-width: 100%;
  margin: 0 auto;
  height: 300px;
  background-color: transparent;
  /* 设置背景色为透明 */
}

.carousel-inner {
  display: flex;
  transition: transform 0.5s ease-in-out;
  height: 100%;
  background-color: transparent;
  /* 设置背景色为透明 */
}

.carousel-item {
  flex: 0 0 100%;
  position: relative;
  height: 100%;
  background-color: transparent;
  /* 设置背景色为透明 */
}

.carousel-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 10px;
}

.marquee {
  display: block;
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  padding: 10px;
  box-sizing: border-box;
  text-align: center;
  animation: marquee 10s linear infinite;
}

@keyframes marquee {
  0% {
    transform: translateX(100%);
  }

  100% {
    transform: translateX(-100%);
  }
}

.contestants {
  text-align: center;
  padding: 10px 0;
  background-color: transparent;
  /* 设置背景色为透明 */
  margin-top: -20px;
}

.contestants-title {
  padding: 5px 10px;
  display: inline-block;
  width: 200px;
  background-color: transparent;
  /* 设置背景色为透明 */
}

.row {
  display: flex;
  justify-content: space-around;
  width: 100%;
  margin-bottom: 20px;
  background-color: transparent;
  /* 设置背景色为透明 */
}

.contestant {
  text-align: center;
  width: 150px;
  margin: 0 10px;

}

.contestant img {
  width: 100%;
  height: auto;
  border-radius: 50%;
}

footer {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  text-align: center;
  padding: 8px 0 0 0;
  background-color: rgb(254, 251, 236);
  box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.1);
  z-index: 1;
}

/* styles.css */



h1 {
  margin-top: -8px;
  background-color: transparent;
  /* 设置背景色为透明 */
}

h2 {
  margin-top: -8px;
  margin-bottom: 5px;
  background-color: transparent;
  /* 设置背景色为透明 */
}

/* 响应式设计 */
@media (max-width: 600px) {

  header {
    text-align: center;
    padding: 5px 0;
    background-color: transparent;
    /* 设置背景色为透明 */
  }

  header h1 {
    font-size: 1.5em;
    background-color: transparent;
    /* 设置背景色为透明 */
  }

  header h2 {
    font-size: 1.5em;
    background-color: transparent;
    /* 设置背景色为透明 */
  }

  .carousel {
    height: 300px;
    background-color: transparent;
    /* 设置背景色为透明 */
  }

  .carousel-item {
    flex: 0 0 100%;
    height: 100%;
    background-color: transparent;
    /* 设置背景色为透明 */
  }

  .carousel-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    position: relative;
  }



  .slide img {
    width: 100%;
    height: 80%;
    object-fit: cover;
    display: block;
  }

  .slider {
    position: relative;
    /* 确保父元素具有相对定位 */
    margin-top: -30px;
    z-index: 2;
  }

  .indicator {
    position: absolute;
    bottom: auto;
    /* 移除底部定位 */
    top: 70%;
    /* 设置顶部定位为70% */
    left: 50%;
    transform: translateX(-50%);
    display: flex;

  }

  .indicator span {
    width: 10px;
    /* 点的宽度 */
    height: 10px;
    /* 点的高度 */
    background-color: #ccc;
    /* 未激活时的颜色 */
    border-radius: 50%;
    /* 圆形 */
    cursor: pointer;
    /* 鼠标悬停改变为手型 */
  }

  .indicator span.active {
    background-color: black;
    /* 激活时的颜色 */
  }

  .slider {
    width: 90%;
    max-width: 100%;
    margin: 0 auto;
    height: 210px;
    position: relative;
    overflow: hidden;
    background-color: transparent;
    /* 设置背景色为透明 */
  }

  .slide {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    opacity: 0;
    transition: opacity 0.5s ease-in-out;
    background-color: transparent;
    /* 设置背景色为透明 */
  }

  .slide.active {
    opacity: 1;
  }

  .caption {
    position: absolute;
    bottom: 40px;
    left: 100px;
    font-size: 30px;
    padding: 5px 10px;
    border-radius: 5px;
    background-color: transparent;
    /* 设置背景色为透明 */
  }

  .contestants-title {
    width: 125px;
    background-color: transparent;
    /* 设置背景色为透明 */
  }
}

.el-dialog {
  width: 300px !important;
  margin-left: auto;
  margin-right: auto;
  background-color: transparent;
  /* 设置背景色为透明 */
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 15px;
  position: relative;
  z-index: 1;
  /* 确保内容在覆盖层之上 */
}

.contestant img {
  width: 138px;
  height: 200px;
  object-fit: cover;
  border-radius: 0;
}

.bottom-tip {
  text-align: center;
  background-color: rgba(0, 0, 0, 0.7);
  padding: 10px 0;
  display: none;
  z-index: 1000;
  background-color: transparent;
  /* 设置背景色为透明 */
  position: absolute;
  bottom: 290px;
  /* 调整这个值以确保紧贴轮播图 */
  left: 0;
  right: 0;
  margin: 0 auto;
  max-width: 1200px;
  /* 与 .container 的宽度一致 */
}

.keyboard {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-top: 20px;
  background-color: transparent;
  /* 设置背景色为透明 */
}

.keyboard button {
  width: 100%;
  height: 50px;
  font-size: 20px;
  background-color: transparent;
  /* 设置背景色为透明 */
}

/* 底色设置 */
.top-decorator::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 150px;
  background-color: transparent;
  /* 设置背景色为透明 */
  border-radius: 50% / 100%;
  transform: translateY(-50%);
  z-index: -1;
}

@media (max-width: 768px) {
  .top-decorator::before {
    height: 100px;
    background-color: transparent;
    /* 设置背景色为透明 */
  }
}

@media (max-width: 480px) {
  .top-decorator::before {
    height: 80px;
    background-color: transparent;
    /* 设置背景色为透明 */
  }

  .bottom-tip {
    text-align: center;
    background-color: rgba(0, 0, 0, 0.7);
    padding: 10px 0;
    display: none;
    z-index: 1000;
    background-color: transparent;
    /* 设置背景色为透明 */
    position: absolute;
    bottom: 310px;
    /* 调整这个值以确保紧贴轮播图 */
    left: 0;
    right: 0;
    margin: 0 auto;
    max-width: 1200px;
    /* 与 .container 的宽度一致 */
  }
}

.bottom-message {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  text-align: center;
  color: white;
  font-size: 14px;
  padding: 10px 0;
  background-color: rgba(0, 0, 0, 0.5);
  /* 半透明背景色 */
  z-index: -1;
  /* 确保它在底部按钮后面 */
}

/* 默认样式 */
body,
html {
  margin: 0;
  padding: 0;
  height: 100%;
  background-image: url('../assets/背景.jpg');
  background-size: cover;
  background-position: top center;
  background-attachment: fixed;
  position: relative;
}

body::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: white;
  z-index: -2;
  /* 确保覆盖层在背景图之下 */
}

body::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: rgb(255, 255, 255);
  height: 100%;
  z-index: -1;
}

@media (max-width: 480px) {

  body,
  html {
    background-size: 100% auto;
  }

  .container {
    height: 100vh;
    /* 设置容器高度为视口高度 */
    display: flex;
    flex-direction: column;
  }

}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 15px;
  position: relative;
  z-index: 1;
  /* 确保内容在覆盖层之上 */
}

.contestant img {
  width: 138px;
  height: 150px;
  object-fit: cover;
  border-radius: 0;
}

.slide img {
  border-radius: 10px;
  /* 添加圆角效果 */
}

.top-background {
  position: relative;
  width: 120%;
  /* 设置宽度为90% */
  height: 200px;
  /* 增加高度 */
  background-color: rgb(217, 185, 162);
  border-bottom-left-radius: 50% 50px;
  border-bottom-right-radius: 50% 50px;
  z-index: 1;
  /* 确保内容在背景模块之上 */
  display: flex;
  /* 使用 flexbox 布局 */
  flex-direction: column;
  /* 垂直排列 */
  align-items: center;
  /* 水平居中 */
  justify-content: center;
  /* 垂直居中 */
  margin: 0 auto;
  /* 使背景模块在水平方向上居中 */
  top: 0;
  /* 确保顶部不偏离 */
  left: 50%;
  /* 确保从左侧50% */
  transform: translateX(-50%);
  /* 确保元素真正居中 */
}

header h2,
header h1 {
  color: white;
  /* 根据需要调整文字颜色 */
  text-align: center;
  /* 文字居中 */
  margin: 0;
  /* 清除默认 margin */
}

header h2 {
  top: 5px;
  /* 调整上边距使其上移 */
}

header h1 {
  top: 5px;
  /* 调整上边距使其上移 */
}


header {
  position: relative;
  z-index: 2;
  /* 确保 header 内容在背景模块之上 */
}



.slider {
  position: relative;
  /* 确保父元素具有相对定位 */
  margin-top: -30px;
  z-index: 2;
}

.indicator {
  position: absolute;
  bottom: auto;
  /* 移除底部定位 */
  top: 70%;
  /* 设置顶部定位为80% */
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 5px;
}

.indicator span {
  width: 10px;
  /* 点的宽度 */
  height: 10px;
  /* 点的高度 */
  background-color: #ccc;
  /* 未激活时的颜色 */
  border-radius: 50%;
  /* 圆形 */
  cursor: pointer;
  /* 鼠标悬停改变为手型 */
}

.indicator span.active {
  background-color: black;
  /* 激活时的颜色 */
}

@media (max-width: 480px) {
  .top-background {
    height: 120px;
    /* 调整手机端的高度 */
  }

  header h2 {
    top: 10px;
    left: 10px;
    font-size: 24px;
    /* 调整手机端的字体大小 */
  }

  header h1 {
    top: 40px;
    left: 10px;
    font-size: 24px;
    /* 调整手机端的字体大小 */
  }
}

body,
html {
  margin: 0;
  padding: 0;
  height: 100%;
  background-image: url('../assets/背景.jpg');
  background-size: cover;
  background-position: top center;
  background-attachment: fixed;
  position: relative;
}

.contestants {
  background-color: rgb(254, 251, 236);
  /* 背景颜色 */
  color: rgb(135, 106, 100);
  /* 字体颜色 */
  padding: 5px 0;
  /* 上下内边距 */
  border-radius: 30px;
  /* 圆角半径 */
  position: relative;
  overflow: hidden;
  /* 隐藏溢出 */
  width: 120px;
  /* 设置宽度为 120px */
  margin: -25px auto;
  /* 居中显示，并加一点顶部外边距 */
  text-align: center;
  /* 胶囊内文本居中 */
}

.contestants-title {
  margin: 0;
  /* 清除默认 margin */
  font-size: 20px;
  /* 字体大小 */
  padding: 5px 0;
  /* 调整内边距，确保文本与外边距有适当间距 */
  display: block;
  /* 使标题块元素 */
}


.contestants::before,
.contestants::after {
  position: absolute;
  top: 0;
  width: 50%;
  height: 100%;
  border-radius: 50%;
  /* 圆形 */
  background-color: rgb(254, 251, 236);
}

.contestants::before {
  left: -20px;
  /* 左半圆 */
}

.contestants::after {
  right: -20px;
  /* 右半圆 */
}


.contestant {
  text-align: center;
  /* 居中内容 */
  margin-bottom: 10px;
  /* 每个选手之间的间距 */
}

.contestant img {
  width: 100px;
  /* 头像宽度 */
  height: 140px;
  /* 头像高度 */
  object-fit: cover;
  /* 适应容器 */
  border-radius: 5px;
  /* 圆角边框 */
}

.pill-button {
  background-color: rgb(254, 251, 236);
  color: rgb(135, 106, 100);
  padding: 5px 20px;
  border-radius: 30px;
  position: relative;
  overflow: hidden;
  width: 100px;
  /* 调整宽度 */
  margin: 5px auto;
  text-align: center;
  cursor: pointer;
  border: none;
  outline: none;
}

.pill-button::before,
.pill-button::after {
  position: absolute;
  top: 0;
  width: 50%;
  height: 100%;
  border-radius: 50%;
  background-color: rgb(254, 251, 236);
}

.pill-button::before {
  left: -20px;
}

.pill-button::after {
  right: -20px;
}

.pill-button.scored {
  background-color: #d3d3d3;
  color: #888888;
  pointer-events: none;
}

footer {
  display: flex;
  justify-content: space-around;
  margin-top: 20px;
}

.home-button,
.score-button {
  width: 40px;
  /* 根据图片大小调整 */
  height: 40px;
  /* 根据图片大小调整 */
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  border: none;
  cursor: pointer;
  outline: none;
  margin-top: 5px;
  position: relative;
  z-index: 9;
}

.home-button {
  background-image: url('../assets/首页.jpg');
  background-color: transparent;
  /* 取消灰色背景 */
  z-index: 9;
  /* 确保首页图标在最上层 */
}

.score-button {
  background-image: url('../assets/查看评分.jpg');
  background-color: transparent;
  /* 取消灰色背景 */
  z-index: 9;
  /* 确保查看评分按钮位于中间 */
}

.exit-button {
  background-image: url('../assets/退出.jpg');
  background-color: transparent;
  /* 取消灰色背景 */
  z-index: 9;
  /* 确保查看评分按钮位于中间 */
  width: 60px;
  /* 根据图片大小调整 */
  height: 60px;
  /* 根据图片大小调整 */
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  border: none;
  cursor: pointer;
  outline: none;
  margin-top: -8px;
  margin-right: -6px;
  position: relative;
}

.home-button1,
.score-button1,
.exit-button1 {
  width: 80px;
  /* 根据图片大小调整 */
  height: 50px;
  /* 根据图片大小调整 */
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  border: none;
  cursor: pointer;
  outline: none;
  position: relative;
  z-index: 8;
}

.home-button1 {
  background-image: url('../assets/深色.jpg');

  margin: 0 auto;
}

.score-button1 {
  background-image: url('../assets/浅色.jpg');


  margin: 0 auto;
}

.exit-button1 {
  background-image: url('../assets/浅色.jpg');

  margin: 0 auto;
}

/* 检查是否有隐藏或不可见的样式 */
.home-button-text,
.exit-button-text,
.score-button-text {
  font-size: 12px;
  /* 小字 */
  color: black;
  /* 黑色 */
  margin-top: 0px;
  /* 调整与按钮的间距 */
  text-align: center;
  /* 文字居中 */

  z-index: 10;
  display: block;
  /* 确保不是 none */
  visibility: visible;
  /* 确保不是 hidden */

}
</style>